﻿using System.Security.Cryptography;
using System.Text;

namespace SNSGame.Cryptography
{
    /// <summary>
    /// SHA1签名生成
    /// </summary>
    public class SHA1
    {
        /// <summary>
        /// SHA1签名(UTF8)
        /// </summary>
        /// <param name="source">被加密的字符串</param>
        /// <returns></returns>
        public static string Encrypt(string source)
        {
            return Encrypt(source, Encoding.UTF8);
        }

        /// <summary>
        /// 对源字符串进行相应的编码后生成SHA1
        /// </summary>
        /// <param name="source"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static string Encrypt(string source,Encoding encoding)
        {
            byte[] strRes = encoding.GetBytes(source);
            HashAlgorithm iSHA = new SHA1CryptoServiceProvider();
            strRes = iSHA.ComputeHash(strRes);
            var sb = new StringBuilder();
            foreach (byte iByte in strRes)
            {
                sb.AppendFormat("{0:x2}", iByte);
            }
            return sb.ToString();
        }

    }
}